package com.lxl.securitypermission.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.lxl.securitypermission.constant.HttpCode;
import com.lxl.securitypermission.dto.ResponseResult;
import com.lxl.securitypermission.dto.ResponseResultPage;
import com.lxl.securitypermission.service.PermissionService;
import com.lxl.securitypermission.service.RoleService;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author 绫小路
 * @Date 2020/12/31
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController {

  @Autowired
  private HttpServletRequest request;

  @Autowired
  private RoleService roleService;
  @Autowired
  private PermissionService permissionService;

  @ApiOperation("角色管理页面")
  @GetMapping(value = {""})
  public String index() {
    return "admin/admin-role";
  }

  @ApiOperation("角色列表")
  @GetMapping("list")
  @ResponseBody
  public ResponseResultPage list(Integer page, Integer limit, String search) {
    if ((page != null && (page < 1 || page > 100)) ||
        (limit != null && (limit < 1 || limit > 100))) {
      return new ResponseResultPage(HttpCode.FAIL, "入参page、limit范围分别是：1~100、1~100");
    }
    if (page == null) {
      page = 1;
    }
    if (limit == null) {
      limit = 10;
    }
    return roleService.getRoleLists(search, page, limit);
  }

  @ApiOperation("获取所有角色")
  @GetMapping("all")
  @ResponseBody
  public ResponseResult all() {
    return new ResponseResult(HttpCode.OK, roleService.getSimpleRoleVo(1));
  }

  @ApiOperation("添加角色")
  @PostMapping("add")
  @ResponseBody
  public ResponseResult add(String name, String description) {
    if (StrUtil.isBlank(name)) {
      return new ResponseResult(HttpCode.FAIL, "角色名称不能为空");
    }
    return roleService.add(name, description);
  }

  @ApiOperation("删除角色")
  @PostMapping("delete")
  @ResponseBody
  public ResponseResult delete(String name) {
    if (StrUtil.isEmpty(name)) {
      return new ResponseResult(HttpCode.FAIL, "入参name不能为空！");
    }
    return roleService.delete(name);
  }

  @ApiOperation("编辑角色")
  @PostMapping("edit")
  @ResponseBody
  public ResponseResult edit(Integer id, String name, String description) {
    Assert.isFalse(id == null, "入参id不能为空！");
    if (StrUtil.isBlank(name)) {
      return new ResponseResult(HttpCode.FAIL, "角色名称不能为空");
    }
    if (description != null && description.length() > 200) {
      return new ResponseResult(HttpCode.FAIL, "描述最长只能200个字符");
    }
    return roleService.edit(id, name, description);
  }

  @ApiOperation("角色授权页面")
  @GetMapping("permission")
  public String permission() {
    return "admin/admin-role-permission";
  }

  @ApiOperation("角色授权列表")
  @GetMapping("permissionList")
  @ResponseBody
  public ResponseResultPage permissionList(Integer page, Integer limit, String search) {
    if ((page != null && (page < 1 || page > 100)) ||
        (limit != null && (limit < 1 || limit > 100))) {
      return new ResponseResultPage(HttpCode.FAIL, "入参page、limit范围分别是：1~100、1~100");
    }
    if (page == null) {
      page = 1;
    }
    if (limit == null) {
      limit = 10;
    }
    return roleService.permissionList(search, page, limit);
  }

  @ApiOperation("删除角色授权")
  @PostMapping("permission/delete")
  @ResponseBody
  public ResponseResult delete(Integer id) {
    Assert.isFalse(id == null, "入参ID不能为空！");
    return roleService.deleteRolePermission(id);
  }

  @ApiOperation("添加角色授权页面")
  @GetMapping("permission/add")
  public String add() {
    request.setAttribute("role", roleService.getSimpleRoleVo(1));
    request.setAttribute("permission", permissionService.getSimplePermissionVo(1));
    return "admin/admin-role-permission-add";
  }

  @ApiOperation("添加角色授权")
  @PostMapping("permission/add")
  @ResponseBody
  public ResponseResult add(Integer roleId, Integer permissionId) {
    Assert.isFalse(roleId == null, "入参roleId不能为空！");
    Assert.isFalse(permissionId == null, "入参permissionId不能为空！");
    return roleService.add(roleId, permissionId);
  }
}
